using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace ClimbTracker.GUI
{
    public partial class CompetitionManagementTree : Form
    {
        private const int COMPETITORS = 0;
        private const int ROUTES = 1;
        private ClimbTrackerDataSet.competitionsDataTable CompetitionsTable;
        private ClimbTrackerDataSet.competitorsDataTable CompetitorsTable;
        private ClimbTrackerDataSet.routesDataTable RoutesTable;


        public CompetitionManagementTree()
        {
            InitializeComponent();
        }

        private void CompetitionManagementTree_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'climbTrackerDataSet.competitors' table. You can move, or remove it, as needed.
            this.competitorsTableAdapter.Fill(this.climbTrackerDataSet.competitors);
            // Fill the parent form's area
            this.Dock = DockStyle.Fill;

            // get references from of tables from main MDI form
            CompetitionsTable = Program.mainMDIForm.CompetitionsTable;
            CompetitorsTable = Program.mainMDIForm.CompetitorsTable;
            RoutesTable = Program.mainMDIForm.RoutesTable;

            populateCompetitors();
        }

        private void populateCompetitors()
        {
            foreach (ClimbTrackerDataSet.competitorsRow row in CompetitorsTable)
            {
                treeView.Nodes[COMPETITORS].Nodes.Add(row.Name);
            }
            foreach (ClimbTrackerDataSet.routesRow row in RoutesTable)
            {
                treeView.Nodes[ROUTES].Nodes.Add(row.Name);
            }
            
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int treeLevel = e.Node.Level;
            if (treeLevel != 0)
            {
                foreach(ClimbTrackerDataSet.competitorsRow row in CompetitorsTable)
                {
                    if(row.Name.Equals(e.Node.Text))
                    {
                        Program.mainMDIForm.ActiveCompetitorID = (int)row.CompetitorID;
                        break;
                    }
                }
            }
        }

    }
}